<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org">
<head th:replace="common/common2 :: common_head(~{::title},~{},~{})">
    <title>统计执行记录</title>
</head>
<body>
<script type="text/javascript">
    var statusStore = Ext.create('Ext.data.Store', {
        fields: ['type', 'name'],
        data: [
            {"type": "未执行", "name": "0"},
            {"type": "已执行", "name": "1"},
            {"type": "执行中", "name": "2"}
        ]
    });


    var taskResultStore = Ext.create('Ext.data.Store', {
        fields: ['type', 'name'],
        data: [
            {"type": "成功", "name": "1"},
            {"type": "失败", "name": "0"},
        ]
    });


    Ext.onReady(function () {
        var model = Ext.create('Ext.data.Model', {
            fields: [
                {
                    type: 'hidden',
                    name: 'id' //id
                }, {
                    type: 'string',
                    name: 'executeQueue' //执行队列
                }, {
                    type: 'string',
                    name: 'ywid' //业务ID
                }, {
                    type: 'string',
                    name: 'fxlx' //分析类型
                }, {
                    type: 'hidden',
                    name: 'profileId' //分析类型id
                }, {
                    type: 'string',
                    name: 'resultQueue' //结果队列
                }, {
                    type: 'string',
                    name: 'costTimeQueue' //查询时耗
                }, {
                    type: 'string',
                    name: 'result' //执行结果
                }, {
                    type: 'string',
                    name: 'errorMsg' //错误信息
                }, {
                    type: 'string',
                    name: 'status' //执行状态
                }, {
                    type: 'string',
                    name: 'errorMsg' //错误信息
                },
            ]
        });

        var analysisTypeStore = Ext.create('Ext.data.Store', {
            autoLoad: true,
            fields: ['type', 'name'],
            proxy: {
                type: 'ajax',
                api: {
                    read: GS.global.ctxPath + '/enum/getAnalysisTypeValues'
                },
                actionMethods: {
                    read: 'POST', // Store设置请求的方法，与Ajax请求有区别
                },
                /**设置读取方式*/
                reader: {
                    type: 'json'
                }
            }
        });

        var store = Ext.create('Ext.data.Store', {
            model: model,
            autoLoad: true,
            pageSize: 20,
            proxy: {
                type: 'ajax',
                api: {
                    read: GS.global.ctxPath + '/logStatistics/list'
                },
                actionMethods: {
                    read: 'POST', // Store设置请求的方法，与Ajax请求有区别
                },
                /**设置读取方式*/
                reader: {
                    type: 'json',
                    rootProperty: 'data.records',
                    totalProperty: 'data.total'
                }
            },
            listeners: {
                'beforeload': function (store, op, options) {
                    /**从头开始*/
                    var params = {
                        query_fxlx:Ext.getCmp('query_fxlx').getValue()
                    };
                    Ext.apply(store.proxy.extraParams, params);
                }
            }
        });

        var action = {
            ADD: 'add',
            UPDATE: 'update',
            CHECK: 'check'
        };

        /**分页*/
        var pageCombo = Ext.create('Ext.form.ComboBox', {
            name: 'pagesize',
            hiddenName: 'pagesize',
            store: new Ext.data.ArrayStore({
                fields: ['text', 'value'],
                data: [
                    ['20', 20], ['50', 50]
                ]
            }),
            valueField: 'value',
            displayField: 'text',
            emptyText: 20,
            width: 50
        });

        //添加下拉显示条数菜单选中事件
        pageCombo.on("change", function (comboBox) {
            var pagingToolbar = Ext.getCmp('pagingbar');
            pagingToolbar.pageSize = parseInt(comboBox.getValue());
            var itemsPerPage = parseInt(comboBox.getValue());//更改全局变量itemsPerPage
            store.pageSize = itemsPerPage;
            store.loadPage(1);
        });

        var grid = Ext.create('Ext.grid.Panel', {
            title: '执行记录',
            tbar: [
                {
                    text: '查看',
                    handler: function () {
                        addLogStatistics(action.CHECK);
                    }
                }, '->',
                {
                    xtype: 'textfield',
                    id: 'query_fxlx',
                    name: 'query_fxlx',
                    emptyText: '请输入分析类型...',
                }, {
                    xtype: 'button',
                    id: 'btnStuListQuery',
                    text: '查询',
                    handler: function () {
                        store.loadPage(1);
                    }

                }, {
                    xtype: 'button',
                    id: 'btnStuListReset',
                    text: '重置',
                    handler: function () {
                        Ext.getCmp('query_fxlx').reset();
                        store.loadPage(1);
                    }
                },
            ],
            dockedItems: [{
                id: 'pagingbar',
                xtype: 'pagingtoolbar',
                store: store,
                dock: 'bottom',
                displayInfo: true,
                autoScroll: true,
                beforePageText: "第",// update
                afterPageText: "页  共 {0} 页",// update
                firstText: "第一页",
                prevText: "上一页",// update
                nextText: "下一页",
                lastText: "最后页",
                refreshText: "刷新",
                displayMsg: "显示 {0} - {1}条，共 {2} 条",// update
                emptyMsg: '没有数据',
                items: ['-', '每页显示', pageCombo, '条']//此处是将创建的combobox添加到工具栏中
            }],
            store: store,
            columns: [
                {
                    xtype: 'rownumberer',
                    header: '行号',
                    renderer: function (value, metadata, record, rowIndex) {
                        return (rowIndex + 1);
                    },
                    width: 60,
                    align: 'center',
                },
                {
                    text: '执行队列',
                    width: 120,
                    dataIndex: 'executeQueue'
                }, {
                    text: '业务ID',
                    width: 120,
                    dataIndex: 'ywid'
                }, {
                    text: '分析类型',
                    width: 150,
                    dataIndex: 'fxlx',
                    store: analysisTypeStore
                },{
                    text: '分析类型名称',
                    width: 150,
                    dataIndex: 'fxlx',
                    store: analysisTypeStore,
                    renderer: function (value) {
                        var displayField = '';
                        analysisTypeStore.getData().items.forEach(function (e) {
                            if (value === e.data.type) {
                                displayField = e.data.name
                            }
                        });
                        return displayField;
                    }
                }, {
                    text: '结果队列',
                    width: 120,
                    dataIndex: 'resultQueue'
                }, {
                    text: '查询时间',
                    width: 160,
                    dataIndex: 'modifiedTime',
                    renderer: function (value) {
                        return Ext.Date.format(new Date(value), 'Y-m-d G:i:s');
                    }
                }, {
                    text: '查询时耗',
                    width: 120,
                    dataIndex: 'costTimeQueue'
                }, {
                    text: '执行结果',
                    width: 120,
                    dataIndex: 'result',
                    renderer: function (value) {
                        var displayField = value;
                        taskResultStore.getData().items.forEach(function (e) {
                            if (value === e.data.name) {
                                displayField = e.data.type
                            }
                        });
                        return displayField;
                    }
                }, {
                    text: '错误信息',
                    flex: 1,
                    dataIndex: 'errorMsg'
                }, {
                    text: '执行状态',
                    width: 120,
                    dataIndex: 'status',
                    renderer: function (value) {
                        var displayField = value;
                        statusStore.getData().items.forEach(function (e) {
                            if (value === e.data.name) {
                                displayField = e.data.type
                            }
                        });
                        return displayField;
                    }
                },

            ],
            listeners: {
                rowdblclick: function () {
                    addLogStatistics(action.CHECK);
                }
            }
        });

        new Ext.Viewport({
            layout: 'fit',
            items: [grid]
        });


        var form = Ext.create('Ext.form.Panel', {
            border: false,
            layout: {type: 'auto', align: 'right'},
            buttonAlign: 'center',
            defaultType: 'textfield',
            bodyStyle: {
                padding: '20px'
            },
            items: [
                {
                    xtype: 'container',
                    layout: 'hbox',
                    items: [
                        {
                            xtype: 'textfield',
                            fieldLabel: '执行队列',
                            name: 'executeQueue',
                            flex: 5,
                            margin: '5 5 5 5',
                            allowBlank: false,
                            maxLength: 25,
                        }, {
                            xtype: 'textfield',
                            fieldLabel: '业务ID',
                            name: 'ywid',
                            flex: 5,
                            margin: '5 5 5 5',
                            allowBlank: false,
                            maxLength: 36,
                        },
                    ]
                },
                {
                    xtype: 'container',
                    layout: 'hbox',
                    items: [
                        {
                            flex: 5,
                            margin: '5 5 5 5',
                            allowBlank: false,
                            xtype: 'combobox',
                            name: 'fxlx',
                            fieldLabel: '分析类型',
                            queryModel: 'local',
                            valueField: 'type',
                            displayField: 'name',
                            store: analysisTypeStore
                        },
                        {
                            xtype: 'textfield',
                            fieldLabel: '结果队列',
                            name: 'resultQueue',
                            flex: 5,
                            margin: '5 5 5 5',
                            allowBlank: false,
                        }
                    ]
                }, {
                    xtype: 'container',
                    layout: 'hbox',
                    items: [
                        {
                            xtype: 'textfield',
                            fieldLabel: '查询时耗',
                            name: 'costTimeQueue',
                            flex: 5,
                            margin: '5 5 5 5',
                            allowBlank: false,
                            maxLength: 25
                        },
                        {
                            xtype: 'combobox',
                            fieldLabel: '执行结果',
                            name: 'result',
                            queryModel: 'local',
                            displayField: 'type',
                            valueField: 'name',
                            flex: 5,
                            margin: '5 5 5 5',
                            allowBlank: false,
                            store: taskResultStore
                        }
                    ]
                }, {
                    xtype: 'container',
                    layout: 'hbox',
                    items: [
                        {
                            xtype: 'combobox',
                            fieldLabel: '执行状态',
                            name: 'status',
                            queryModel: 'local',
                            displayField: 'type',
                            valueField: 'name',
                            width: '49%',
                            margin: '5 5 5 5',
                            allowBlank: false,
                            store: statusStore
                        }
                    ]
                }, {
                    xtype: 'container',
                    layout: 'hbox',
                    items: [
                        {
                            xtype: 'textareafield',
                            fieldLabel: '错误信息',
                            name: 'errorMsg',
                            flex: 10,
                            height: 160,
                            margin: '5 5 5 5',
                            allowBlank: false
                        }
                    ]
                },

            ]
        });

        var profileWindow = Ext.create('Ext.Window', {
            title: 'Mongo日志',
            width: 600,
            height: 450,
            resizable: false,
            modal: true,
            items: [form],
            maximizable: true,
            minimizable: true,
            closeAction: 'hide',//设置点击关闭按钮为隐藏,不设置，window不能重复利用
            buttons: [{
                xtype: 'button', text: '确定', handler: function () {
                    saveLogStatistics();
                }
            }, {
                xtype: 'button', text: '取消', handler: function () {
                    closeWindow();
                }
            }]
        });

        function addLogStatistics(actionType) {
            var title;
            setModalReadOnly(false);
            if (actionType === action.ADD) {
                title = '新增';
            } else if (actionType === action.UPDATE) {
                title = '修改';
                var record = getSelectionRecord();
                if (!record) {
                    Ext.MessageBox.alert('提示', '只能选中一条数据进行处理!');
                    return;
                }
                // 为模态窗赋值
                form.getForm().setValues(record.data);
            } else if (actionType === action.CHECK) {
                title = '查看';
                var record = getSelectionRecord();
                if (!record) {
                    Ext.MessageBox.alert('提示', '只能选中一条数据进行处理!');
                    return;
                }
                // 为模态窗赋值
                form.getForm().setValues(record.data);
                setModalReadOnly(true);
            }
            /**设置标题**/
            profileWindow.setTitle(title);
            profileWindow.show();
        }


        function setModalReadOnly(flag) {
            form.getForm().getFields().each(function (field) {
                //设置只读
                field.setReadOnly(flag);
            });
            var saveButton = profileWindow.query('.button')[0];
            if (flag) {
                saveButton.hide();
            } else {
                saveButton.show();
            }
        }

        // 关闭模态窗，清空模态窗中表单数据
        function closeWindow() {
            form.getForm().reset();
            profileWindow.hide();
        }

        /***
         * 获取选中的数据,只提供单选模式
         */
        function getSelectionRecord() {
            var records = grid.selModel.getSelection();
            if (records.length === 1) {
                return records[0];
            } else {
                return null;
            }
        }

        function saveLogStatistics() {
            var form_ = form.getForm();
            var actionType = form_.findField('actionType').getValue();
            form.getForm().findField('modifiedTime').setValue(new Date());
            if (form_.isValid()) {
                var url, msg;
                if (actionType === action.UPDATE) {
                    url = GS.global.ctxPath + "/logStatistics/update";
                    msg = '更新成功!';
                } else if (actionType === action.ADD) {
                    url = "/logStatistics/add";
                    msg = '添加成功!';
                }
                form_.submit({
                    method: 'POST',
                    url: url,
                    waitMsg: '请稍等,正在执行',
                    params: form_.getValues(),
                    success: function (form, action) {
                        closeWindow();
                        grid.store.reload();
                        Ext.MessageBox.alert('提示', msg);
                    },
                    failure: function (form, action) {
                        Ext.MessageBox.alert('提示', "操作失败");
                    }

                })
            }
        }

        // 删除配置分析文件
        function deleteLogStatistics() {
            var record = getSelectionRecord();
            if (!record) {
                Ext.MessageBox.alert('提示', '只能选中一条数据进行处理!');
                return;
            }
            Ext.Msg.show({
                title: '提示',
                message: '是否确认删除？',
                buttons: Ext.Msg.YESNO,
                icon: Ext.Msg.QUESTION,
                fn: function (btn) {
                    if (btn === 'yes') {
                        Ext.Ajax.request({
                            method: 'POST',
                            url: GS.global.ctxPath + '/logStatistics/delete',
                            params: {
                                'id': record.get("adjustId")
                            },
                            success: function (response, config) {
                                Ext.Msg.alert('提示', '删除成功!');
                                grid.store.reload();//重新加载数据
                            },
                            failure: function () {
                                Ext.Msg.alert('提示', '删除失败!');
                            }
                        });
                    }

                }
            })
        }
    });
</script>
</body>
</html>